//
//  ZFMapConfigModal.swift
//  map
//
//  Created by fyzq on 2024/2/18.
//

import UIKit
import RealmSwift
/**
 图层配置
 */
class ZFMapConfigModal: Object {
    @Persisted(primaryKey: true) var uuid: String
    @Persisted var enableRotate:Bool = true ///是否可以旋转
    @Persisted var enablePitch:Bool = true ///是否可以3D
    @Persisted var enableRoad:Bool = false ///是否显示路网
    @Persisted var enablePlace:Bool = false ///是否显示路标
    @Persisted var enableElevation:Bool = false ///是否显示等高线
    @Persisted var enableRecordCoordinate:Bool = false ///是否显示上一次位置
    @Persisted var coordinate:String? ///最后一次位置(经纬度）
    @Persisted var point:ZFCenterPointConfig?
    @Persisted var compass:ZFCompassConfig?
    
}
/** 中心十字图片配置  */
class ZFCenterPointConfig : Object {
    @Persisted(primaryKey: true) var _id: ObjectId
    @Persisted var showImage:Bool = true ///是否显示中心图片
    @Persisted var imageUrl:String?
    @Persisted var imageSize:Float = 40
}

///** compass 罗盘配置  */
class ZFCompassConfig : Object {
    @Persisted(primaryKey: true) var _id: ObjectId
    @Persisted var showImage:Bool = true ///是否显示中心图片
    @Persisted var imageUrl:String?
    @Persisted var imageSize:Float = 40
}

extension ZFMapConfigModal {
    
    static func setupDefault(){
        let uuid = String.UUID()
        
        let predicate = NSPredicate(format: "uuid == %@", uuid)
        guard let configs = ZFRealmManager.objectsWithPredicate(object: ZFMapConfigModal.self, predicate: predicate),configs.count > 0 else {
            ///查询不到用户 创建用户
            return onCreateMapConfig(uuid: uuid)
        }

    }
    
    fileprivate static func onCreateMapConfig(uuid:String){
        let config =  ZFMapConfigModal()
        config.uuid = uuid
        config.point = ZFCenterPointConfig()
        config.compass = ZFCompassConfig()
        ZFRealmManager.add(config) {
            print("添加配置成功")
        }
    }
    
    static func getMapConfig() -> ZFMapConfigModal? {
        guard let list = ZFRealmManager.objects(ZFMapConfigModal.self) as? [ZFMapConfigModal] else{
            return nil
        }
        return list.last
    }
    
}
